<?php namespace App\Services\FindModuleService;
/**
 * 发现页API
 *
 * 发现页service
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
use App\Services\BaseService;
use Illuminate\Support\Facades\Log;
use App\Services\UserService\CommendService;
use App\Services\ProductsService\ProductsService;
use App\Services\CarouselsService\CarouselsService;
use App\Services\TypesService\TypesService;
use App\Services\ProductTempService\ProductTempTool;
use App\Services\ProdTempCategoryService\ProdTempCategoryTool;
use App\Services\Tool\Car\CarTool;
use App\Model\Announcements;
class FindModuleService extends BaseService{

    protected $commendService;
    protected $productsService;
    protected $carouselsService;
    protected $typesService;
    protected $productTempTool;
    protected $carTool;
    public function __construct(CommendService $commendService,ProductsService $productsService,CarouselsService $carouselsService,TypesService $typesService,ProductTempTool $productTempTool,ProdTempCategoryTool $prodTempCategoryTool,CarTool $carTool)
    {
        $this->commendService = $commendService;
        $this->productsService = $productsService;
        $this->typesService = $typesService;
        $this->carouselsService = $carouselsService;
        $this->productTempTool = $productTempTool;
        $this->prodTempCategoryTool = $prodTempCategoryTool;
        $this->carTool = $carTool;
    }

    /**
     * 获取模块列表
     * @return array
     */
    public function getList($user_id = 0){
        try{
            $this->setParameter(array(
                'table'=>'find_modules',
                'where'=>array(array('state','=',1),array('is_show','=',1)),
                'select'=>array('find_module_id','find_module_name','app_id','redirect_module'),
                'sort'=>'desc','order'=>'sort'
            ));
            $findModules = $this->getAll()->toArray();
            $data = array();
            foreach($findModules as $findModule){
                $findModuleData = array();
                $findModuleData['title'] = $findModule->find_module_name;
                $findModuleData['app_id'] = $findModule->app_id;
                $findModuleData['product_temp_category_id'] = intval($findModule->redirect_module);
                $findModuleData['name'] = intval($findModule->redirect_module) == 0 ? '全部' : $this->prodTempCategoryTool->productTempCategoryNameByProdTempCategoryId($findModule->redirect_module);
                $app_name = $this->getAppNameByAppid($findModule->app_id);
                switch($app_name){
                    case 'carousels':
                        $findModuleData['data'] = $this->carouselsService->getList();
                        break;
                    case 'types':
                        //获取产品分类 获取推荐分类
                        $this->setParameter(array(
                            'table'=>'find_module_resources',
                            'where'=>array(array('find_module_id','=',$findModule->find_module_id), array('state','=',1)),
                            'select'=>'resource_id',
                            'order'=>'sort',
                            'sort'=>'desc'
                        ));
                        $types = $this->getProductListByModuleId($this->typesService,50);
                        $findModuleData['data'] = $types['data'];
                        break;
                    case 'users':
                        $this->setParameter(array('table'=>'find_module_resources',
                            'where'=>array(
                                array('find_module_id','=',$findModule->find_module_id),
                                array('state','=',1)),
                            'select'=>'resource_id',
                            'order'=>'sort',
                            'sort'=>'desc'
                        ));
                        $user_ids = $this->getListByModuleId(50);
                        $users = $this->commendService->commendDesignerToIdex($user_id,10,$user_ids['data']);
                        $findModuleData['data'] = $users;
                        break;
                    case 'brands' :
                        $this->setParameter(array(
                            'table'=>'find_module_resources',
                            'where'=>array(
                                array('find_module_id','=',$findModule->find_module_id),
                                array('state','=',1)
                            ),
                            'select'=>'resource_id',
                            'order'=>'sort',
                            'sort'=>'desc'
                        ));
                        $user_ids = $this->getListByModuleId(50);
                        $users = $this->commendService->commendBrandToIndex($user_id,10,$user_ids['data']);
                        $findModuleData['data'] = $users;
                        break;
                    case 'products':
                        $this->setParameter(array('table'=>'find_module_resources',
                            'where'=>array(
                                array('find_module_id','=',$findModule->find_module_id),
                                array('state','=',1)),
                            'select'=>'resource_id',
                            'order'=>'sort',
                            'sort'=>'desc'
                        ));
                        $products = $this->getProductListByModuleId($this->productsService,20);
                        $findModuleData['data'] = $products['data'];
                        break;
                    case 'announcements':
                        $announcements = Announcements::where(['state'=>1])
                            ->where('announcement_begin_time','<',time())
                            ->where('announcement_end_time','>=',time())
                            ->select('announcement_id','announcement_content','announcement_type',
                                'announcement_begin_time','announcement_end_time')
                            ->orderBy('updated_at','desc')
                            ->get()
                            ->toArray();
                        foreach($announcements as $key=>$announcement){
                            $announcements[$key]['url'] = $announcement['announcement_type']== 2 ?
                                asset('api/announcements/detail/'.$announcement['announcement_id']) : '';
                        }
                        $findModuleData['data'] = $announcements;
                        break;
                }
                if(!empty($findModuleData['data'])){
                    $data['data'][] = $findModuleData;
                }
            }
            //获取模板分类
            $product_temp_categorys = $this->productTempTool->getAllProductTempCategorys(1);
            //array_unshift($product_temp_categorys,(object)array('name'=>'全部','product_temp_category_id'=>0));
            $data['category'] = $product_temp_categorys;
            $data['good_settlement_quantity'] = empty($user_id) ? 0 : $this->carTool->get_shop_car_quantity(array('user_id'=>$user_id),2)['good_settlement_quantity'];
            $data['announcement'] = '';
            return $this->createResult('M00000',$data);
        }catch(\Exception $e){
            Log::info($e);
           return $this->createResult('M00000',(object)array());
        }
    }
}